package org.jeecg.modules.pickOrder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.DateUtils;
import org.jeecg.constant.WarehouseConstant;
import org.jeecg.modules.inOrder.entity.InOrder;
import org.jeecg.modules.inOrder.service.IInOrderService;
import org.jeecg.modules.pickOrder.entity.PickOrder;
import org.jeecg.modules.pickOrder.entity.PickOrderDetail;
import org.jeecg.modules.pickOrder.mapper.PickOrderDetailMapper;
import org.jeecg.modules.pickOrder.mapper.PickOrderMapper;
import org.jeecg.modules.pickOrder.service.IPickOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 理货管理
 * @Author: jeecg-boot
 * @Date: 2024-10-30
 * @Version: V1.0
 */
@Service
public class PickOrderServiceImpl extends ServiceImpl<PickOrderMapper, PickOrder> implements IPickOrderService {

    @Resource
    private PickOrderMapper pickOrderMapper;
    @Resource
    private PickOrderDetailMapper pickOrderDetailMapper;
    @Resource
    private IInOrderService inOrderService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePickOrder(PickOrder pickOrder, List<PickOrderDetail> pickOrderDetailList) {
        Assert.isTrue(StringUtils.isNotBlank(pickOrder.getContainerNo()), "请输入集装箱号！");
        Assert.notNull(pickOrder.getGoodsType(), "请选择品番类型！");
        // 过滤全部为空的数据
        pickOrderDetailList = pickOrderDetailList.stream().filter(data -> !(StringUtils.isBlank(data.getStackCode()) && StringUtils.isBlank(data.getSkuCode()) && null == data.getBoxNumber() && null == data.getGoodsNumber())).collect(Collectors.toList());
        // 校验必填字段
        long count = pickOrderDetailList.stream().filter(data -> StringUtils.isBlank(data.getStackCode()) || StringUtils.isBlank(data.getSkuCode()) || null == data.getBoxNumber() || null == data.getGoodsNumber()).count();
        Assert.isTrue(count == 0, "货垛码、品番编码、箱子数量、物品数量必须填写！");

        if (StringUtils.isBlank(pickOrder.getPickOrderCode())) {
            pickOrder.setPickOrderCode(DateUtils.getToday() + "-" + System.currentTimeMillis());
        } else {
            int orderCodeCount = inOrderService.count(new LambdaQueryWrapper<InOrder>()
                    .eq(InOrder::getInOrderCode, pickOrder.getPickOrderCode())
                    .ne(InOrder::getStatus, WarehouseConstant.EnterWarehouseEnum.DELETE.getCode()));
            Assert.isTrue(orderCodeCount == 0, "订单号【%s】已存在！", pickOrder.getPickOrderCode());
        }

        if (null == pickOrder.getPickOrderTime()) {
            pickOrder.setPickOrderTime(new Date());
        }

        if (StringUtils.isBlank(pickOrder.getId())) {
            this.saveMain(pickOrder, pickOrderDetailList);
        } else {
            this.updateMain(pickOrder, pickOrderDetailList);
        }
    }

    @Override
    @Transactional
    public void saveMain(PickOrder pickOrder, List<PickOrderDetail> pickOrderDetailList) {
        pickOrder.setStatus(WarehouseConstant.PickWarehouseTaskStaffEnum.CREATE_END.getCode());
        pickOrderMapper.insert(pickOrder);
        if (CollectionUtils.isNotEmpty(pickOrderDetailList)) {
            for (PickOrderDetail entity : pickOrderDetailList) {
                entity.setPickOrderId(pickOrder.getId());
                pickOrderDetailMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(PickOrder pickOrder, List<PickOrderDetail> pickOrderDetailList) {
        pickOrderMapper.updateById(pickOrder);

        //1.先删除子表数据
        pickOrderDetailMapper.deleteByMainId(pickOrder.getId());

        //2.子表数据重新插入
        if (CollectionUtils.isNotEmpty(pickOrderDetailList)) {
            for (PickOrderDetail entity : pickOrderDetailList) {
                entity.setPickOrderId(pickOrder.getId());
                pickOrderDetailMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        PickOrder pickOrder = pickOrderMapper.selectById(id);
        Assert.notNull(pickOrder, "理货单数据异常，无法删除！");
        pickOrder.setStatus(WarehouseConstant.PickWarehouseTaskStaffEnum.DELETE.getCode());
        pickOrderMapper.updateById(pickOrder);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            this.delMain(id.toString());
        }
    }

}
